package ControlLayer;
import java.util.ArrayList;
import TUILayer.AddressBookUI;
import ModelLayer.*;
//model layer package has to be imported

/**
 * Controller class for the address book
 * 
 * @author (kbh) 
 * @version (2005.03.31)
 * (FEN): 2006.03.21: Some changes to the architecture in order to assure layering
 *                    and loose coupling
 */
public class AddressCtr
{
    
    private AddressBookUI adrUI;
    private AddressBook addressBookModel;
    private Person person;

    public AddressCtr()//DVD container is to be passed as parameter from the main controller
    {
        adrUI = new AddressBookUI();
        addressBookModel = AddressBook.getInstance();
        addressBookMenu();
    }

    public void addressBookMenu() {
        boolean exit = false;
        
        while(!exit)//that is: while exit is false
        {
            adrUI.writeAddressBookMenu();
            int choise = this.adrUI.readInt();
            
            if(choise == 1) {
                // Show information about person
                showPersonDetails();
            } else if(choise == 2) {
                // Add person
                createPerson();
            } else if(choise == 3) {
                // Delete person
                deletePerson();
            } else if(choise == 4) {
                // Update person
                updatePerson();
            } else if(choise == 5) {
                // List all persons
                listAllPersons();
            } else {
                // Return
                exit = true;
            }
        }
    }
    
    //Dummy implementation: after implementing the model layer 
    //objects of class Person are to be returned, not String
    public Person getPerson(int id)
    {
        Person person = addressBookModel.findPerson(id);
        return person;
    }
    
    public void showPersonDetails() {
        int id = personSelect();
        Person person = addressBookModel.findPerson(id);
        
        if(person != null) {
            adrUI.writePersonDetails(person);
            adrUI.writeEnterToContinue();
            adrUI.readLine();
        }
    }
    
    public void createPerson()
    {   
        adrUI.clearConsole();
        adrUI.writeCreateNewPerson();
        
        // ID, int
        adrUI.writeInputPersonsID();
        int id = adrUI.readInt();
        
        // Name, string
        adrUI.writeInputName();
        String name = adrUI.readLine();
        
        // Address, string
        adrUI.writeInputAddress();
        String address = adrUI.readLine();
        
        // Zipcode, int
        adrUI.writeInputPostalCode();
        int zipcode = adrUI.readInt();
        
        // City, string
        adrUI.writeInputCity();
        String city = adrUI.readLine();
        
        // Phone, string
        adrUI.writeInputPhone();
        String phone = adrUI.readLine();
        
        // Creates a new person object
        Person person = new Person(id, name, address, zipcode, city, phone);

        // Saves person in addressbook.
        addressBookModel.addPerson(person);
        
        adrUI.writeBlankLine();
        adrUI.writeSaveOK();
        adrUI.writeEnterToContinue();
        adrUI.readLine();
    }
    
    public void deletePerson()
    {
        int id = personSelect();
        
        Person person = addressBookModel.findPerson(id);
        if(person != null) {
            addressBookModel.deletePerson(id);
        }
    }
    
    public void updatePerson()
    {
        String name;
        String address;
        int zipcode;
        String city;
        String phone;
        
        int id = personSelect();
        
        Person person = addressBookModel.findPerson(id);
        if(person != null) {
        
            adrUI.writeInputName(person.getName());
            name = adrUI.readLine();
            if(name.length() > 0) {
                person.setName(name);
            }
        
            adrUI.writeInputAddress(person.getAddress());
            address = adrUI.readLine();
            if(address.length() > 0) {
                person.setAddress(address);
            }
        
            adrUI.writeInputPostalCode(person.getZipcode());
            zipcode = adrUI.readInt();
            if(zipcode > 0) {
                person.setZipcode(zipcode);
            }
        
            adrUI.writeInputCity(person.getCity());
            city = adrUI.readLine();
            if(city.length() > 0) {
                person.setCity(city);
            }
        
            adrUI.writeInputPhone(person.getPhone());
            phone = adrUI.readLine();
            if(phone.length() > 0) {
                person.setPhone(phone);
            }
        
        }
    }
    
    private int personSelect()
    {
        int id = 0;
        adrUI.writeAddressbook();
        ArrayList<Person> p = addressBookModel.getAllPersons();
        adrUI.writeOutPersons(p);
        
        if(p.size() > 0) {
            adrUI.writeSelectPersonId();
        
            id = adrUI.readInt();
        } else {
            adrUI.writeEnterToContinue();
            adrUI.readLine();
        }
        return id;
    }
    
    public void listAllPersons()
    {
        adrUI.writeAddressbook();
        adrUI.writeOutPersons(addressBookModel.getAllPersons());
        
        adrUI.writeBlankLine();
        adrUI.writeEnterToContinue();
        adrUI.readLine();
    }
}
